/**
 * Copyright 2011 Joe Choi, All rights reserved.
 */
package tao.data.structure.tree;

/**
 * The implementation of Builder interface
 * 
 * @author Joe Choi
 */
public class ConcreteBuilder<S, V, T extends Tree<V>> implements Builder<V, T> {
	public ConcreteBuilder(Handler<S, V, T> handler) {
		this.handler = handler;
	}
		
	@Override
	public final Builder<V, T> build() {
		Traversal<S> traversal = handler.getTraversal();
		traversal.register(handler);
		traversal.start();
		return(this);
	}
	
	@Override
	public T getTarget() {
		return(handler.getRoot());
	}
		
	private Handler<S, V, T> handler;
	
	/**
	 * Basic implementation of Handler interface
	 */
	public abstract static class AbstractHandler<S, V, T extends Tree<V>> extends Traversal.Adaptor<S> 
		implements Builder.Handler<S, V, T> {
		
		@Override
		public void setSource(Tree<S> source) {
			this.source = source;
		}
		
		@Override
		public T getRoot() {
			return(this.root);
		}
		
		protected T root;
		protected Tree<S> source;			
	}	
}